import datetime
from app.core.extensions import db


class RecognitionRecord(db.Model):
    __tablename__ = 'recognition_records'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False, comment='用户ID')
    username = db.Column(db.String(50), nullable=True, comment='用户名')
    real_name = db.Column(db.String(50), nullable=True, comment='真实姓名')
    recognition_type = db.Column(db.String(20), nullable=False,
                                 comment='识别类型：face-人脸识别，id_card-身份证，drive_license-驾驶证')
    recognition_result = db.Column(db.String(20), nullable=False, comment='识别结果：success-成功，failed-失败')
    success_rate = db.Column(db.Float, nullable=True, comment='识别率（百分比）')
    recognition_image = db.Column(db.String(255), nullable=True, comment='识别图像URL')
    photo_url = db.Column(db.String(255), nullable=True, comment='用户拍摄的原始照片URL')
    recognition_data = db.Column(db.Text, nullable=True, comment='识别详细数据（JSON）')
    device_info = db.Column(db.Text, nullable=True, comment='设备信息')
    ip_address = db.Column(db.String(50), nullable=True, comment='IP地址')
    location = db.Column(db.String(255), nullable=True, comment='地理位置')
    created_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    tenant_id = db.Column(db.Integer, db.ForeignKey('tenants.id'), nullable=True, comment='租户ID')

    def to_dict(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'username': self.username,
            'real_name': self.real_name,
            'recognition_type': self.recognition_type,
            'recognition_result': self.recognition_result,
            'success_rate': self.success_rate,
            'recognition_image': self.recognition_image,
            'photo_url': self.photo_url,
            'recognition_data': self.recognition_data,
            'device_info': self.device_info,
            'ip_address': self.ip_address,
            'location': self.location,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            'tenant_id': self.tenant_id
        }
